package main

import (
	"fmt"
	"github.com/DeanThompson/ginpprof"
	"github.com/gin-gonic/gin"
	"github.com/mix-go/dotenv"
	"log"
	controllers "mytest/flower-order/controller"
	"net/http"
	"os"
	"path/filepath"
	"runtime/debug"
)

func main() {

	r := gin.Default()
	r.Use(recover500)
	//处理404 请求
	r.NoRoute(recover400)

	//r.HTMLRender = ginmultitemplate.LoadTemplateFiles("common-admin/views", ".html", funcMap)
	r.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"), "src/my-test/flower-order/template/**/*")) //将模板文件打包到编译文件中
	r.Static("/static", filepath.Join(os.Getenv("GOPATH"), "src/my-test/flower-order/static"))   //将静态文件打包到编译文件中
	r.StaticFS("/img", http.Dir(dotenv.Getenv("UPLOAD_PATH").String()))                          //图片地址

	loadAdminRoutes(r)

	ginpprof.Wrap(r)

	err := r.Run(":8080")
	if err != nil {
		panic(fmt.Sprintf("启动失败, err : %s ", err))
	}
}

func recover400(c *gin.Context) {
	//c.JSON(200, gin.H{
	//	"status":  404,
	//	"message": "404 Not Found" + c.Request.Method + " " + c.FullPath(),
	//})
	c.HTML(http.StatusNotFound, "error/404.tmpl", nil)
}

func recover500(c *gin.Context) {
	defer func() {
		if r := recover(); r != nil {
			log.Printf("panic: %v\n", r)
			debug.PrintStack()
			c.JSON(200, gin.H{
				"code":    500,
				"message": "服务器内部错误",
			})
		}
	}()
	c.Next()
}

func loadAdminRoutes(r *gin.Engine) {
	var orderController controllers.OrderController
	var categoryController controllers.HuashuCategoryController
	var huashuController controllers.HuashuController
	var userController controllers.UserController
	var imgController controllers.ImgController
	var loginController controllers.LoginController
	var configController controllers.ConfigController
	var statisticsController controllers.StatisticsController

	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "order/index.tmpl", gin.H{
			"title": "Users",
		})
	})
	//-----------------------------------验证码----------------------------------
	r.GET("/captcha", loginController.Captcha)
	r.GET("/captcha/verify", loginController.VerifyCaptcha)

	//-----------------------------------统计面板----------------------------------
	r.GET("/panel", statisticsController.Index)

	//-----------------------------------配置----------------------------------
	r.GET("/config/index", configController.Index)
	r.POST("/config/edit", configController.Edit)
	r.POST("/config/getConfigByKey", configController.GetConfigByKey)

	//-----------------------------------图片----------------------------------
	r.GET("/image/index", imgController.Index)
	r.POST("/image/upload", imgController.Upload)
	r.POST("/image/multiUpload", imgController.MultiUpload)
	r.POST("/image/createFolder", imgController.CreateFolder)
	r.POST("/image/delete", imgController.Delete)

	//-----------------------------------订单-------------------------------------
	r.GET("/order/index", orderController.Index)
	r.POST("/order/add", orderController.Add)
	r.GET("/order/show", orderController.Show)
	r.POST("/order/edit", orderController.Edit)

	//-----------------------------------分类-------------------------------------
	r.GET("/category/index", categoryController.Index)
	r.POST("/category/add", categoryController.Add)
	r.GET("/category/show", categoryController.Show)
	r.POST("/category/edit", categoryController.Edit)

	//-----------------------------------花束-------------------------------------
	r.GET("/huashu/index", huashuController.Index)
	r.POST("/huashu/add", huashuController.Add)
	r.GET("/huashu/show", huashuController.Show)
	r.POST("/huashu/edit", huashuController.Edit)

	//-----------------------------------用户-------------------------------------
	r.GET("/user/index", userController.Index)
	r.POST("/user/add", userController.Add)
	r.GET("/user/show", userController.Show)
	r.POST("/user/edit", userController.Edit)

}
